# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------#
# ZhihuiJin <zhihuijin@gmail.com>
# vim: ts=4 sw=4 sts=4 et tw=78:
# Fri Dec  8 14:46:39 2006
#
#----------------------------------------------------------------------------#

""" An interface of output
"""

#----------------------------------------------------------------------------#

from tools.common import sopen, filterComments
from tools.table import parseLines

#----------------------------------------------------------------------------#
def readList(filename):
    """ return the character list in file
    """
    list = [line.rstrip() for line in filterComments(sopen(filename))]
    return list

#----------------------------------------------------------------------------#
def writeList (outList, filename):
    """ write the list to file
    """
    outFile = sopen(filename, 'w')
    for word in outList:
        print>>outFile, word
    outFile.close()

#----------------------------------------------------------------------------#
def readDict(filename, sep=' '):
    """ read the dict from file
    """
    dictObj = {}
    for key, value in parseLines(sopen(filename), sep, n=2):
        dictObj[key] = value

    return dictObj

#----------------------------------------------------------------------------#
def writeDict(outDict, filename, sep=' '):
    """ write the dict to file
    """
    outFile = sopen(filename)
    for word in outDict.keys():
        print>>outFile, word + sep + outDict[word]
    outFile.close()

#----------------------------------------------------------------------------#
def commList(firstList, secondList):
    """ return the intersection of two list
    """
    commSet = set(firstList).intersection(set(secondList))
    return list(commSet)

#----------------------------------------------------------------------------#
def prodList(firstList, secondList):
    """ return the Cartesian product of two list
    """
    if len(firstList) == 0:
        return secondList
    if len(secondList) == 0:
        return firstList

    result = []
    for a in firstList:
        for b in secondList:
            result.append(a + b)

    return result

#----------------------------------------------------------------------------#

# vim: ts=4 sw=4 sts=4 et tw=78:
